package com.dmall.opening.up.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

public class Page<T> implements JsonType {

    private final long totalEntriesCount;

    private final long page;

    private final long pageSize;

    private final List<T> entries;

    public Page(long totalEntriesCount, long page, long pageSize, List<T> entries) {
        this.totalEntriesCount = totalEntriesCount;
        this.page = page;
        this.pageSize = pageSize;
        this.entries = entries;
    }

    @Override
    public Map<String, Object> toJson() {
        Map<String, Object> entity = new HashMap<>();
        entity.put("entries", entries.stream().map(e -> (e instanceof JsonType) ? ((JsonType) e).toJson() : e).collect(toList()));
        entity.put("total", totalEntriesCount);
        entity.put("page_size", pageSize);
        entity.put("page", page);
        return entity;
    }
}
